package com.ssh;


public class FindMedianSortedArrays_4 {
	
	public static void main(String[] args) {
		int[] a = new int[] {1,2};
		int[] b = new int[] {3,4};
		System.out.println(findMedianSortedArrays(a, b));
		
		
	}
	
	/**
	 * 寻找有序数组的中位数,定义两个数组的总长度的一半的数组，挨着加入
	 * @param nums1
	 * @param nums2
	 * @return
	 */
	public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
		int x = 0, y = 0;
		double mid = 0.0;
		int N = nums1.length + nums2.length;
		double[] n = new double[N/2+1];
		for (int i = 0; i <= N/2; i++) {
			if (x == nums1.length) {
				n[i] = nums2[y++];
			}else if (y == nums2.length) {
				n[i] = nums1[x++];
			}else {
				n[i] = nums1[x] <= nums2[y] ? nums1[x++] : nums2[y++];
			} 
		}
		if (N % 2 == 0) {
			mid = (n[N/2] + n[N/2-1])/2;
		}else {
			mid = n[N/2];
		}
		return mid;
	}
}
/*if (nums1.length < i) {
mid = nums2[] 
}else if (nums2.length < i) {
x++;
}else if (nums1[x] < nums2[y]) {
x++;
}else if (nums1[x] >= nums2[y]) {
y++;
}
if (x+y == N/2) {

}*/